/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Data.Type;
using Core.Model.Events;

namespace Core.Model.Data.Variables
{
    public class ObjectRefVariable : Variable
    {

        #region  Exchanged Attributes

        private ObjectRef originalValue;

        public ObjectRef OriginalValue
        {
            get { return originalValue; }
            set { originalValue = value; }
        }

        #endregion

        #region  Internal Attributes

        private ObjectRef value;

        public override void InittInternalAttributes ()
        {
            base.InittInternalAttributes();

            value = originalValue;
        }

        #endregion

        #region  Actions

        public void SetVariable ( ObjectRef value )
        {
            this.value = value;
        }

        public void TestVariable ( ComparasonTests op, ObjectRef comparisonValue )
        {
            bool res = false;

            switch ( op )
            {
                case ComparasonTests.Equal:
                    res = ( value.Equals( comparisonValue ) );
                    break;

                case ComparasonTests.NotEqual:
                    res = ( !value.Equals( comparisonValue ) );
                    break;

                default:
                    base.TestVariable( op, comparisonValue );
                    return;
            }

            MHEGEngine.Instance.Handle( new TestEvent( this, res ) );
        }

        public override bool Equals ( Object o )
        {
            if ( this == o ) return true;
            if ( !( o is ObjectRefVariable ) ) return false;

            //const
            //ObjectRefVariable objectRefVariable = ( ObjectRefVariable ) o;

            return ( Value == ( ( ObjectRefVariable ) o ).Value );
            //if ( value != null ? !value.Equals( objectRefVariable.value ) : objectRefVariable.value != null ) return false;

           // return true;
        }

        public override int GetHashCode ()
        {
            return ( Value != null ? Value.GetHashCode() : 0 );
        }

        public ObjectRef Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public override object Data
        {
            get { return this.value; }
            set { this.value =(ObjectRef) value; }
        }

        public override string ToString ()
        {
            return base.ToString() +":"+Value;
        }
        #endregion
    }
}
